package com.credit.jumu.calculator.base;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.credit.jumu.calculator.utils.LogUtils;
import com.umeng.analytics.MobclickAgent;

import org.androidannotations.annotations.AfterViews;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/11/22/022.
 */

public class BaseActivity extends AppCompatActivity {
	private Window window;
	InputMethodManager imm;

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public void initView() {
		toAllWindow();
	}

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public void toAllWindow() {
		if (null == window) {
			window = this.getWindow();
		}
//		StatusBarUtil.setColor(this, getResources().getColor(R.color.red));
		//StatusBarCompat.compatTransNavigationBar(getActivity());
		//设置透明状态栏,这样才能让 ContentView 向上
		window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//有半透明
//		window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);//底部也全屏了
//		window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		//需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
//		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		ViewGroup mContentView = (ViewGroup) this.findViewById(Window.ID_ANDROID_CONTENT);
		View mChildView = mContentView.getChildAt(0);
		if (mChildView != null) {
			//注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
			ViewCompat.setFitsSystemWindows(mChildView, false);
		}
	}

	public int getNavigationBarHeight() {
		int screenHeight = getAccurateScreenDpi()[1];
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int heightDifference = screenHeight - dm.heightPixels;
		LogUtils.println("导航栏高度" + "Navi height:" + heightDifference);
		return heightDifference;
	}

	/**
	 * 获取精确的屏幕大小
	 */
	public int[] getAccurateScreenDpi() {
		int[] screenWH = new int[2];
		Display display = getWindowManager().getDefaultDisplay();
		DisplayMetrics dm = new DisplayMetrics();
		try {
			Class<?> c = Class.forName("android.view.Display");
			Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
			method.invoke(display, dm);
			screenWH[0] = dm.widthPixels;
			screenWH[1] = dm.heightPixels;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return screenWH;
	}

	public int getStatusBarHeight() {
		int statusBarHeight = -1;
		//获取status_bar_height资源的ID
		int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
		if (resourceId > 0) {
			//根据资源ID获取响应的尺寸值
			statusBarHeight = getResources().getDimensionPixelSize(resourceId);
		}
		LogUtils.println("顶部导航栏高度" + statusBarHeight);
		return statusBarHeight;
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	protected void hideKeyboard() {
		if (null == imm) {
			imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		}
		if (imm.isActive() && this.getCurrentFocus() != null) {
			if (this.getCurrentFocus().getWindowToken() != null) {
				imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}
}
